//
//  JMRGBTextView.m
//  MasterBoard
//
//  Created by 赵俊明 on 2018/12/28.
//  Copyright © 2018 赵俊明. All rights reserved.
//

#import "JMRGBTextView.h"
#import "JMBaseColorView.h"
#import "UIColor+Wonderful.h"
#import "JMSlider.h"

#import "UIImage+JMImage.h"
#import "UIView+Extension.h"
#import "JMMacro.h"
#import "StaticClass.h"

@interface JMRGBTextView()
@property (nonatomic, weak) UILabel *r_RGB;
@property (nonatomic, weak) UILabel *g_RGB;
@property (nonatomic, weak) UILabel *b_RGB;
@property (nonatomic, weak) UILabel *a_ALPHA;
@property (nonatomic, weak) JMBaseColorView *baseColorView;
@property (nonatomic, weak) JMSlider *fontSize;
@property (nonatomic, weak) JMSlider *alphaView;
@end

@implementation JMRGBTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        UILabel *r_RGB = [[UILabel alloc] init];
        r_RGB.font = [UIFont systemFontOfSize:11.0];
        r_RGB.textColor = [UIColor whiteColor];;
        r_RGB.textAlignment = NSTextAlignmentCenter;
        [self addSubview:r_RGB];
        self.r_RGB = r_RGB;
        
        UILabel *g_RGB = [[UILabel alloc] init];
        g_RGB.font = [UIFont systemFontOfSize:11.0];
        g_RGB.textColor = [UIColor whiteColor];;
        g_RGB.textAlignment = NSTextAlignmentCenter;
        [self addSubview:g_RGB];
        self.g_RGB = g_RGB;
        
        UILabel *b_RGB = [[UILabel alloc] init];
        b_RGB.font = [UIFont systemFontOfSize:11.0];
        b_RGB.textColor = [UIColor whiteColor];;
        b_RGB.textAlignment = NSTextAlignmentCenter;
        [self addSubview:b_RGB];
        self.b_RGB = b_RGB;
        
        UILabel *a_ALPHA = [[UILabel alloc] init];
        a_ALPHA.text = [NSString stringWithFormat:@"Alpha：%.1f", [StaticClass getAlpha]];
        a_ALPHA.font = [UIFont systemFontOfSize:11.0];
        a_ALPHA.textColor = [UIColor whiteColor];;
        a_ALPHA.textAlignment = NSTextAlignmentCenter;
        [self addSubview:a_ALPHA];
        self.a_ALPHA = a_ALPHA;
        
        JMSelf(ws)
        JMBaseColorView *baseColorView = [[JMBaseColorView alloc] init];
        [self addSubview:baseColorView];
        self.baseColorView = baseColorView;
        baseColorView.colorBlock = ^(UIColor *color) {[ws set_RGBWithColor:color];};
        
        JMSlider *fontSize = [[JMSlider alloc] initWithFrame:CGRectMake(10, 0, self.width-20, 35)];
        fontSize.currentValue = [StaticClass getLineWidth];
        __weak __typeof(&*fontSize)weakSlider = fontSize;
        fontSize.sliderValue = ^(CGFloat value) {
            
            if (ws.sliderValue) {
                ws.sliderValue(value*20, NO);
            }
            weakSlider.title = [NSString stringWithFormat:@"%.0f", value*20];
            [StaticClass setLineWidth:value*20];
        };
        
        [self addSubview:fontSize];
        self.fontSize = fontSize;
        
        JMSlider *alphaView = [[JMSlider alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(fontSize.frame), self.width-20, 35)];
        alphaView.currentValue = [StaticClass getAlpha];
        [self addSubview:alphaView];
        self.alphaView = alphaView;
        alphaView.sliderValue = ^(CGFloat value) {
            if (ws.sliderValue) {
                ws.sliderValue(value, YES);
            }
            [StaticClass setAlpha:value];
            a_ALPHA.text = [NSString stringWithFormat:@"Alpha：%.1f", value];
        };
        
        [self set_RGBWithColor:[StaticClass getColor]];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGFloat y = 0;
    CGFloat w = self.width/4;
    CGFloat height = self.height/4;
    _r_RGB.frame = CGRectMake(0, y, w, height);
    _g_RGB.frame = CGRectMake(CGRectGetMaxX(_r_RGB.frame), y, w, height);
    _b_RGB.frame = CGRectMake(CGRectGetMaxX(_g_RGB.frame), y, w, height);
    _a_ALPHA.frame = CGRectMake(CGRectGetMaxX(_b_RGB.frame), y, w, height);
    _baseColorView.frame = CGRectMake(0, CGRectGetMaxY(_a_ALPHA.frame), self.width, height);
    _fontSize.frame = CGRectMake(10, CGRectGetMaxY(_baseColorView.frame), self.width-20, height);
    _alphaView.frame = CGRectMake(10, CGRectGetMaxY(_fontSize.frame), self.width-20, height);
}

- (void)setColor_Alpha:(NSString *)color_Alpha
{
    _color_Alpha = color_Alpha;
    _a_ALPHA.text = [NSString stringWithFormat:@"Alpha：%@", color_Alpha];
}

- (void)set_RGBWithColor:(UIColor *)color
{
    if (!color) {color = [UIColor redColor];}
    [StaticClass setColor:color];
    const CGFloat *components = CGColorGetComponents(color.CGColor);
    int R = components[0] * 255;
    int G = components[1] * 255;
    int B = components[2] * 255;
    
    _r_RGB.text = [NSString stringWithFormat:@"Red：%d",R];
    _g_RGB.text = [NSString stringWithFormat:@"Green：%d",G];
    _b_RGB.text = [NSString stringWithFormat:@"Blue：%d",B];
}

- (void)chouseColor:(UIButton *)sender
{
//    if (self.colorBlock) {self.colorBlock(sender.backgroundColor);}
}

- (void)dealloc
{
#ifdef DEBUG
    NSLog(@"%@销毁 %s", NSStringFromClass([self class]),__FUNCTION__);
#endif
}

@end
